package nodes;

import compiler.Compiler;
import compiler.SymbolTable;

import descriptors.AbstractDescr;

public class WhileNode extends StatementNode {

	private ExpressionNode expression;
	private ListNode<StatementNode> statementSeq;
	
	public WhileNode(ExpressionNode expression,ListNode<StatementNode> statementSeq,int line,int column){
		super(line,column);
		this.expression=expression;
		this.statementSeq=statementSeq;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public AbstractDescr compile(
			SymbolTable symbolTable, Compiler compiler) {
		int startLabel=compiler.newLabel();
		int endLabel=compiler.newLabel();
		compiler.write("LABEL, "+startLabel);
		expression.compile(symbolTable, compiler);
		compiler.write("BF, "+endLabel);
		statementSeq.compile(symbolTable, compiler);
		compiler.write("JMP, "+startLabel);
		compiler.write("LABEL, "+endLabel);
		return null;
	}

	@Override
	public void print() {
		trace("WhileNode");
		trace("While");
		expression.print();
		unindent();
		for(StatementNode sn:statementSeq){
			sn.print();
		}
		unindent();
	}

}
